<?php
/**

 */
class Client_ManagerController extends App_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
		$user_id = $this->getUserId();
		$user_role = 'client';
    	
		$form2=new Form_ManagerSearchForm(array('id'=>$user_id));
		$this->view->form2=$form2;
					
		$model_client = new Model_UserClient();		
		$managerInfo = $model_client->getManager($user_id);

		if (empty($managerInfo['dpt_ids'])){ //manager not set dpt ids
			$this->view->error_msg="Sorry, you are not assigned to any departments yet. Please contact your supervisor.";								
		}else{
	    	$request=$this->getRequest();
			$data = $request->getParams();
								
			$act = $request->getParam('act');
			if (isset($act)and($act=='disable')){
				$id = $request->getParam('id');		
				
				$auth = $model_client->getManagerClientAuth($user_id,$id);
				if ($auth){
					$result = $model_client->disableClient($id);	
					if ($result)
					{
					   $this->view->success_msg="Client has been disabled!";
					}else{
					   $this->view->error_msg="Sorry, disable client failed.";					
					}		
				}else{
					$this->view->error_msg="Sorry, authority failed.";										
				}
			}
			if (isset($act)and($act=='suspend')){
				$id = $request->getParam('id');		
				$auth = $model_client->getManagerClientAuth($user_id,$id);
				if ($auth){
					$result = $model_client->suspendClient($id);	
					if ($result)
					{
					   $this->view->success_msg="Client has been suspended!";
					}else{
					   $this->view->error_msg="Sorry, suspend client failed.";					
					}		
				}else{
					$this->view->error_msg="Sorry, authority failed.";										
				}
			}
			if (isset($act)and($act=='activate')){
				$id = $request->getParam('id');		
				$auth = $model_client->getManagerClientAuth($user_id,$id);
				if ($auth){
					$result = $model_client->activateClient($id);	
					if ($result)
					{
					   $this->view->success_msg="Client has been activated!";
					}else{
					   $this->view->error_msg="Sorry, activate client failed.";					
					}		
				}else{
					$this->view->error_msg="Sorry, authority failed.";										
				}
			}
			
			$start = $request->getParam('page',1);
			$email = $request->getParam('email');
			$dpt_id = $request->getParam('dpt_id');
			$dpt_account_login = $request->getParam('dpt_account_login');
			$login_area = $request->getParam('login_area');
			$role = $request->getParam('role');

			$item_per_page = 20;		
			$data['start'] = ($start-1)*$item_per_page;
			$data['limit'] = $item_per_page;
			$this->view->start=$data['start'];
			//if($request->isPost())
			{
				if (isset($data['submit']) and ($data['submit']=='Set'))
				{
				}else{
					if ($email)
					{
						$data['filters'][0]['field'] = 'email';
						$data['filters'][0]['data']['type'] = 'string';  //string, list, boolean, numeric, date
						$data['filters'][0]['data']['value'] = $email;
					}
	
					if (($dpt_id)and($dpt_id!=-1))
					{
						if (in_array($dpt_id, $managerInfo['dpt_ids'])) {
							$data['filters'][1]['field'] = 'dpt_id';
							$data['filters'][1]['data']['type'] = 'string';  //string, list, boolean, numeric, date
							$data['filters'][1]['data']['value'] = $dpt_id;
						}else{
							$data['filters'][1]['field'] = 'dpt_id';
							$data['filters'][1]['data']['type'] = 'string';  //string, list, boolean, numeric, date
							$data['filters'][1]['data']['value'] = $managerInfo['dpt_ids'][0];							
						}
					}else{
						$data['filters'][1]['field'] = 'dpt_id';
						$data['filters'][1]['data']['type'] = 'string';  //string, list, boolean, numeric, date
						$data['filters'][1]['data']['value'] = $managerInfo['dpt_ids'][0];													
					}
					
					if ($dpt_account_login)
					{
						$data['filters'][2]['field'] = 'dpt_account_login';
						$data['filters'][2]['data']['type'] = 'string';  //string, list, boolean, numeric, date
						$data['filters'][2]['data']['value'] = $dpt_account_login;
					}
					
					if (($login_area)and($login_area!='any'))
					{
						$data['filters'][3]['field'] = 'login_area';
						$data['filters'][3]['data']['type'] = 'string';  //string, list, boolean, numeric, date
						$data['filters'][3]['data']['value'] = $login_area;
					}
					
					if (($role)and($role!='any'))
					{
						$data['filters'][4]['field'] = 'role';
						$data['filters'][4]['data']['type'] = 'string';  //string, list, boolean, numeric, date
						$data['filters'][4]['data']['value'] = $role;
					}
					
					$form2->populate($data);
					
					
				}
				
			}
					
			$all_clients = $model_client->getAllClient($data);   
			$this->view->all_clients=$all_clients['data'];
			//Zend_Debug::dump($this->view->all_clients);exit;
			
	        $model_service = new Model_Service();
	    	$all_services = $model_service->getAllService();                
			$this->view->all_services=$all_services;        
						
			$_GET['page']=$start;
			$_GET['email']=$email;
			$_GET['dpt_id']=$dpt_id;
			$_GET['dpt_account_login']=$dpt_account_login;
			$_GET['login_area']=$login_area;
			$_GET['role']=$role;
						
			Zend_View_Helper_PaginationControl::setDefaultViewPartial('pagination.phtml');
	    	
	    	$paginator = Zend_Paginator::factory($all_clients['total']);
	    	$paginator->setItemCountPerPage($item_per_page); //num per page
	    	$paginator->setCurrentPageNumber($start);
	    	$this->view->paginator = $paginator;		
		}
		    	
    }


    public function toClientAction()
    {
	  $user_id = $this->getUserId();
	  $user_role = 'client';
    	
      $client_id = $this->getRequest()->getParam('id');
      
	  $model_client = Model_UserFactory::create('client');
      $auth = $model_client->getManagerClientAuth($user_id,$client_id);
	  if ($auth){      
	      $client = $model_client->getUser($client_id);
	      
	      //Zend_Debug::dump($client); exit;
	    	 
	      $session = $this->getSession();
	      $storage = new App_Auth_Storage_Session($session,'client');
	      //Zend_Debug::dump($storage->read());
	      $storage->write(array(
	                    'user_id'=>$client_id,
	                    'user_name'=>$client['email'],
	                    'user_role'=>$client['role']));
	      //Zend_Debug::dump($storage->read()); 
	
	      $this->_redirect("/client");
	  }else{
	  	 echo "Sorry, you don't have privilege to access this account. Please contact your supervisor.";
	  	 exit;
	  }
    }
    
	public function logoutAction()
	{
		$session = $this->getSession();
		$session->unsetAll();

		$this->_redirect('/');
	}

    
}